package com.songshuai.pangu.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.songshuai.pangu.api.component.SalesClientComponent;
import com.songshuai.pangu.api.dto.pangu.GetSaleCustomerBodyListResDto;
import com.songshuai.pangu.api.dto.pangu.GetSaleCustomerListResDto;
import com.songshuai.pangu.api.service.SalesClientService;
import com.songshuai.webapi.baseapi.dto.pangu.request.salesclient.CustomerListReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.response.salesclient.CustomerInfoDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SalesClientServiceImpl implements SalesClientService {

    @Autowired
    private SalesClientComponent salesClientComponent;

    @Override
    public QueryResponse<List<CustomerInfoDto>> getCustomerList(BaseParmDto<CustomerListReqDto> request) {


        String customerList = salesClientComponent.queryGetSaleCustomerList(request);
        log.info("queryGetSaleCustomerList:jsonResultParam{}",customerList);

        if (StringUtils.isNotBlank(customerList)) {

            QueryRes<GetSaleCustomerListResDto> queryRes = JSON.parseObject(customerList, new TypeReference<QueryRes<GetSaleCustomerListResDto>>() {});

            if (ResultStatus.SUCCESS.getErrorCode() == queryRes.getStatus()) {

                List<CustomerInfoDto> lists = new ArrayList<CustomerInfoDto>();
                if (!CollectionUtils.isEmpty(queryRes.getResult().getBody())) {

                    GetSaleCustomerListResDto result = queryRes.getResult();
                    CustomerInfoDto customerInfoDto = null;
                    for (GetSaleCustomerBodyListResDto e : result.getBody()) {
                        customerInfoDto = new CustomerInfoDto();
                        customerInfoDto.setCustomerId(e.getCustomerGuid());
                        customerInfoDto.setCustomerName(e.getCustomerName());
                        customerInfoDto.setIsBinding(e.getIsBindWebchat());
                        customerInfoDto.setMobile(e.getMainPhone());
                        customerInfoDto.setStatus(e.getClueStatusName());
                        lists.add(customerInfoDto);
                    }

                }

                return QueryResponse.setResData(lists);

            } else {
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(), queryRes.getInfo());
            }
        }

        return new QueryResponse(ResultStatus.FAILOfficial);
    }
}
